package telas.cadastros;

import controller.SituacaoQuartoController;
import java.sql.SQLException;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.SituacaoQuartoModel;

/**
 *
 * @author WELISSON
 */
public class SituacaoQuarto extends javax.swing.JDialog {
    controller.SituacaoQuartoController quarto;
    
    /**
     * Creates new form CadastroSituacaoQuartos
     * @param parent
     * @param modal
     */
    public SituacaoQuarto(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        quarto = new controller.SituacaoQuartoController();
        setLocationRelativeTo(null);
        listaSituacoes();
        desabilitaBotoes();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTCodigo = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTListSituacao = new javax.swing.JTable();
        jBAdcionar = new javax.swing.JButton();
        jBRemover = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTSituacao = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastrar Situação dos Quartos");

        jLabel1.setText("Código:");

        jTListSituacao.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Indice", "Situação"
            }
        ));
        jTListSituacao.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTListSituacaoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTListSituacao);

        jBAdcionar.setText("Adicionar");
        jBAdcionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAdcionarActionPerformed(evt);
            }
        });

        jBRemover.setText("Remover");

        jLabel2.setText("Situação:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBAdcionar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jBRemover)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jBAdcionar, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBRemover)
                .addContainerGap(38, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBAdcionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAdcionarActionPerformed
        String codigo = "",situacao = "";
        codigo = jTCodigo.getText();
        situacao = jTSituacao.getText();
        
        if(codigo.trim().equals("")){
            JOptionPane.showMessageDialog(this, "Informe o Codigo!");
        }else if(codigo.trim().toCharArray().length != 3){
            JOptionPane.showMessageDialog(this, "O código deve ter  3 caracteres!");
        }else if(situacao.trim().equals("")){
            JOptionPane.showMessageDialog(this, "Informe a Situação!");
        }else{
            try {
                if(new SituacaoQuartoController().salvar(codigo, situacao)){
                    JOptionPane.showMessageDialog(this, "Cadastro Realizado com Sucesso!");
                    listaSituacoes();
                    limpaDados();
                }else{
                    JOptionPane.showMessageDialog(this, "Erro ao Cadastrar.\n Verifique se o CODIGO digitado ja existe");
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(this, "Erro ao Cadastrar.\n Tente novamente mais tarde!");
            }
        }
        
    }//GEN-LAST:event_jBAdcionarActionPerformed

    private void jTListSituacaoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTListSituacaoMouseClicked
        acoesTabela();
    }//GEN-LAST:event_jTListSituacaoMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAdcionar;
    private javax.swing.JButton jBRemover;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTCodigo;
    private javax.swing.JTable jTListSituacao;
    private javax.swing.JTextField jTSituacao;
    // End of variables declaration//GEN-END:variables

    private void listaSituacoes(){
        List<SituacaoQuartoModel> situacao = null;
        try {
            situacao = quarto.listaSituacaoQuarto();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        DefaultTableModel dtm = (DefaultTableModel) jTListSituacao.getModel();
                
        jTListSituacao.removeAll();
        dtm.setNumRows(0);
        for(int i = 0; i < situacao.size(); i++){
            dtm.addRow(new Object[]{situacao.get(i).getCodigo(),situacao.get(i).getDescricao()});
        }
        
    }
    
    private void acoesTabela(){
        int linha = jTListSituacao.getSelectedRow();
        if(linha != -1){
            habilitaBotoes();
        }
    }
    
    private void limpaDados(){
        jTCodigo.setText("");
        jTSituacao.setText("");
    }
    
    private void habilitaBotoes(){
        jBRemover.setEnabled(true);
    }
    
    private void desabilitaBotoes(){
        jBRemover.setEnabled(false);
    }
}
